// pages/waiter/ordergoods/ordergoods.js
const request = require('@/utils/request.js')
const constants = require('@/utils/khyConstants.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabValue: '0',
    dataList: [],
    allDataList: [],
    pageNo: 1,
    pageSize: constants.pageSize,
    total: 0,
    packageStateList: getApp().globalData.dictData['packageState'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.tabValue == '1') {
      this.setData({
        allDataList: [],
        pageNo: 1,
        total: 0,
      })
    }
    
    this.getPageData(this.data.tabValue, 1)
  },

  getPageData(type, pageNo) {
    let userInfo = getApp().getUserInfo()
    if (type == '0') {
      let param = {packageState: type}
      if (userInfo.stationId) {
        param.startStation = userInfo.stationId
      }
      request.get('/UserController/getOrderByPackageState', param).then(res => {
        if (res.data.code == constants.resultSuccessCode
          && res.data.result) {
            let data = res.data.result || []
            this.setData({
              dataList: data
            })
        }
      })
    } else {
      let dataCenter = {
        pageNo: pageNo,
        pageSize: this.data.pageSize,
      }
      
      dataCenter.queryForm = [{field: 'tallyClerk', val: userInfo.userNo}]
      request.post('/UserController/pageOrder', dataCenter).then(res => {
        if (res.data.code == constants.resultSuccessCode
          && res.data.result) {
            let data = res.data.result.records || []
            let total = res.data.result.total || 0
            if (data.length > 0) {
              data = this.data.allDataList.concat(data)
              this.setData({
                allDataList: data,
                total: total
              })
            }
        }
      })


    }
  },

  onTabsChange(e) {
    this.setData({
      tabValue: e.detail.value
    })
    this.onShow()
  },
  // ordercheck() {
  //   wx.navigateTo({
  //     url: '/pages/waiter/ordercheck/ordercheck',
  //   })
  // },
  
  print(e){
    let order = e.currentTarget.dataset.order
    if (order) {
      getApp().globalData.pageData[constants.page_waiterprint] = order
      wx.navigateTo({
        url: '/pages/waiter/print/print',
      })
    }
    
  },
  
  orderinfo(e) {
    let order = e.currentTarget.dataset.order
    if (order) {
      getApp().globalData.pageData[constants.page_orderinfo] = order
      wx.navigateTo({
        url: '/pages/orderinfor/orderinfor',
      })
    }
  },

 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.tabValue == '1') {
      let pageNo = this.data.pageNo;
      let total = this.data.total
      let pageNum = total % this.data.pageSize > 0 ? parseInt(total / this.data.pageSize) + 1 : parseInt(total / this.data.pageSize)
      if (pageNo < pageNum) {
        pageNo = pageNo + 1
        this.getPageData(this.data.tabValue, pageNo)
        this.setData({
          pageNo: pageNo
        })
      }
    }
    

    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})